package com.smartrobot.smartrobot.pagerHolder;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.activity.AboutUsAcitivity;
import com.smartrobot.smartrobot.activity.PersonalMessageActivity;
import com.smartrobot.smartrobot.activity.SettingAcitivity;
import com.smartrobot.smartrobot.activity.ShareSoftAcitivity;
import com.smartrobot.smartrobot.activity.ShowBigQrActivity;
import com.smartrobot.smartrobot.util.QRCode;
import com.smartrobot.smartrobot.util.User;

import org.json.JSONObject;

public class PersonalPagerHolder1 implements BasePagerHolder,View.OnClickListener{

	private View view;
	private Context context;
	private boolean isFirst = true;
    private TextView createOrgan,progressTxt,nickName;
    private LinearLayout centerLayout;
    private RelativeLayout aboutUs,personMessage,shareSoft,setting;
    static public Bitmap qrBitmap;

	private ImageView qrCodeImg;
	QRCode qrCode ;

	public PersonalPagerHolder1(Context context, View view) {
		this.context = context;
		this.view = view;
	} 
	
	@Override
	public void setView(View view) {
		this.view = view;
	}

	@Override
	public View getView() {
		return view;
	}
	
	public void initView(){
		
		if(isFirst){
			isFirst = false;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {


			qrCodeImg = (ImageView)view.findViewById(R.id.center_qrCode);
            progressTxt = (TextView)view.findViewById(R.id.person_progressTxt);
            centerLayout = (LinearLayout)view.findViewById(R.id.center_layout);
            nickName = (TextView)view.findViewById(R.id.center_nickName);
            aboutUs = (RelativeLayout)view.findViewById(R.id.center_about_us);
            shareSoft = (RelativeLayout)view.findViewById(R.id.center_share_soft);
            setting = (RelativeLayout)view.findViewById(R.id.center_settting);
            personMessage = (RelativeLayout)view.findViewById(R.id.center_personal_message);
//            createOrgan = (TextView)view.findViewById(R.id.create_organ);

			qrCode = new QRCode();
            if(User.getInstance() != null  && !User.getInstance().getId().equals("未登录")){//先看一下是否登录了

				JSONObject user = new JSONObject();
				try{
					user.put("ID",User.getInstance().getId());
					user.put("USER_ACCOUNT",User.getInstance().getPhone());
					user.put("NICK_NAME",User.getInstance().getNickname());

				}catch (Exception e){
					e.printStackTrace();
					Log.e("===========用户信息转json时出错========",e.getMessage());
				}

                qrBitmap = qrCode.createQRImage(user.toString());
                qrCodeImg.setImageBitmap(qrBitmap);
                if(!User.getInstance().getNickname().equals("未登录"))
                  nickName.setText(User.getInstance().getNickname());
                else
                    nickName.setText(User.getInstance().getPhone());
                qrCodeImg.setVisibility(View.VISIBLE);

            }else if(User.getUserInfo(context)  && !User.getInstance().getId().equals("未登录")){//没有登录就看一下配置文件
                System.out.println("查找数据成功");
                qrBitmap = qrCode.createQRImage(User.getInstance().toString());
                qrCodeImg.setImageBitmap(qrBitmap);
                if(!User.getInstance().getNickname().equals("未登录"))
                    nickName.setText(User.getInstance().getNickname());
                else
                    nickName.setText(User.getInstance().getPhone());
                qrCodeImg.setVisibility(View.VISIBLE);
            }else//都没有就不显示二维码
            {
                System.out.println("无法读入数据");
                nickName.setText("未登录");
                qrCodeImg.setVisibility(View.GONE);
            }



            progressTxt.setVisibility(View.GONE);
            centerLayout.setVisibility(View.VISIBLE);
            qrCodeImg.setOnClickListener(PersonalPagerHolder1.this);
            aboutUs.setOnClickListener(PersonalPagerHolder1.this);
            personMessage.setOnClickListener(PersonalPagerHolder1.this);
            setting.setOnClickListener(PersonalPagerHolder1.this);
//            createOrgan.setOnClickListener(PersonalPagerHolder.this);

                }
            },500);

		}
		
	}

    @Override
    public void onClick(View view) {

        switch(view.getId()){
//            case R.id.create_organ:
//
//                Intent intent = new Intent();
//                intent.setClass(context, CretaOrganActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                context.startActivity(intent);
//
//                break;

            case R.id.center_qrCode:

                Intent intent = new Intent();
                intent.setClass(context, ShowBigQrActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);

                break;

            case R.id.center_about_us:

                Intent aboutIntent = new Intent();
                aboutIntent.setClass(context, AboutUsAcitivity.class);
                aboutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(aboutIntent);

                break;

            case R.id.center_share_soft:

                Intent shareIntent = new Intent();
                shareIntent.setClass(context, ShareSoftAcitivity.class);
                shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(shareIntent);

                break;

            case R.id.center_settting:

                Intent settingIntent = new Intent();
                settingIntent.setClass(context, SettingAcitivity.class);
                settingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(settingIntent);

                break;

            case R.id.center_personal_message:

                Intent centerIntent = new Intent();
                centerIntent.setClass(context, PersonalMessageActivity.class);
                centerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(centerIntent);

                break;

        }

    }
}
